package main

import (
	"fmt"
	"sync"
	"time"
)
var wg sync.WaitGroup
// 加入互斥锁
var rwLock sync.RWMutex

func read() {
	defer wg.Done()
	rwLock.RLock() // 读数据锁不产生影响
	fmt.Println("开始读数据")
	time.Sleep(time.Second)
	fmt.Println("读取数据成功")
	rwLock.RUnlock()
}
func write() {
	defer wg.Done()
	rwLock.Lock()
	fmt.Println("开始写数据")
	time.Sleep(time.Second * 10)
	fmt.Println("数据写成功")
	rwLock.Unlock()
}

func main() {
	wg.Add(6) // 每次启动就+2
	// 测试读多写少
	for i := 0; i < 5; i++ {
		go read()
	}
	go write()
	wg.Wait()
}
